/*
	       File: 3DES.cs
	Create Date: 2007-09-11
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.Text;

namespace GuerillaProgrammer.FileProcessor.Encryption
{
    class _T3DES
    {
        #region Private member fields
        #endregion

        #region Constructors and cleanup methods
        #endregion

        #region Private implementation methods
        public _T3DES()
        {
        }
        #endregion

        #region Class properties
        #endregion

        #region Events handlers
        #endregion

        #region Public interface
        private static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey, string sIV)
        {
            // The default 3DES key size under the .NET framework is 192 (168).  The following
            // call will create a 3DES crypto provider and create a random initialization
            // vector and key. The crypto mode defaults to CBC ensuring the proper chaining 
            // of data to mitigate repetition of cipher text blocks.


            TripleDESCryptoServiceProvider tdes = (TripleDESCryptoServiceProvider)TripleDESCryptoServiceProvider.Create();


            // One could override the default randomly generated key and initialization vector
            // by setting the following properties to the Rijndael Crypto Provider, e.g.:
            //
            //  tdes.Key = some_key_val;
            //  tdes.IV = some_iv_val;
            //  tdes.KeySize = 192;  
            // 
            // Similarly a developer may wish to change the default encryption mode, e.g.:
            //
            //  tdes.Mode = CipherMode.CFB; // Defaults to CBC mode. Use of ECB mode is not recommended
            // as it provides no feedback chaining of ciphertext


            //Set secret key For 3DES algorithm.
            tdes.Key = ASCIIEncoding.ASCII.GetBytes(sKey);


            //Set initialization vector.
            tdes.IV = ASCIIEncoding.ASCII.GetBytes(sIV);


            FileStream fsPlaintext = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
            FileStream fsCiphertext = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);


            //Create an 3DES encryptor from the TripleDES instance.
            ICryptoTransform tdesencrypt = tdes.CreateEncryptor();


            //Create crypto stream set to read and do an 3DES encryption transform on incoming bytes.
            CryptoStream cipherstream = new CryptoStream(fsCiphertext, tdesencrypt, CryptoStreamMode.Write);


            byte[] bytearrayinput = new byte[fsPlaintext.Length];
            fsPlaintext.Read(bytearrayinput, 0, bytearrayinput.Length);
            cipherstream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cipherstream.Close();
            fsPlaintext.Close();
            fsCiphertext.Close();
        }
        #endregion
    }
}
